//
//  SettingDetailSandTableViewController.m
//  宜居HD
//
//  Created by 陈柏兵 on 16/5/4.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "SettingDetailSandTableViewController.h"
#import "Masonry.h"
#import "Macro.h"
#import "UIButton+ColorandText.h"
#import "UIColor+RandomColor.h"
#import "UIButton+NavRightBtn.h"
#import "SandTableCell.h"
#import "DetailSandTableViewController.h"
#import "SettingViewController.h"
#import "AddSandTableViewController.h"

@interface SettingDetailSandTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UIButton *editingBtn;//编辑按钮
@property (nonatomic, strong)UITableView *settingTable;//tableView
@property (nonatomic, strong)UIButton *uploadBtn;//上传按钮
@property (nonatomic, strong)UIButton *downloadBtn;//下载按钮
@property (nonatomic, strong)NSMutableArray *sandTitleArray;//沙盘titleArray
@property (nonatomic, strong)UIView *footerView;
@end

static NSString *identifier = @"SandCell";

@implementation SettingDetailSandTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"沙盘设置";
    self.view.backgroundColor = kSettingBackgroundColor;
    NSString *title1 = @"建筑模型设置";
    NSString *title2 = @"区位沙盘设置";
    NSString *title3 = @"项目沙盘设置";
    [self sandTitleArray];
    [_sandTitleArray addObject:title1];
    [_sandTitleArray addObject:title2];
    [_sandTitleArray addObject:title3];
    
    [self LayOutUI];
}

#pragma - mark UITableView Delegate
//sectiond底部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return SCREEN_HEIGHT*0.14;
}
//section底部试图View
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    _footerView = [[UIView alloc] init];
    _footerView.backgroundColor = [UIColor clearColor];
    _footerView.userInteractionEnabled = YES;
    
    [self uploadBtn];
    [self downloadBtn];
    
    _uploadBtn.frame = CGRectMake(0, SCREEN_HEIGHT * 0.07, SCREEN_WIDTH * 0.26, (SCREEN_HEIGHT * 0.07));
    _downloadBtn.frame = CGRectMake(_settingTable.frame.size.width - SCREEN_WIDTH * 0.26, SCREEN_HEIGHT * 0.07, SCREEN_WIDTH * 0.26, (SCREEN_HEIGHT * 0.07));
    return _footerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sandTitleArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SandTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SandTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = _sandTitleArray[indexPath.row];
    
    if (indexPath.row == _sandTitleArray.count - 1 && _editingBtn.selected == YES) {
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#6ccac9"];
        cell.delateBtn.hidden = YES;
    } else {
        
        if (_editingBtn.selected != YES) {
            cell.isShowBtn = YES;
            cell.textLabel.textColor = [UIColor blackColor];
        } else {
            cell.isShowBtn = NO;
        }
        
    }
    
    cell.delateAction = ^{
        DDLogInfo(@"删除");
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_editingBtn.titleLabel.text isEqualToString:@"保存"] && _sandTitleArray.count - 1 == indexPath.row) {            AddSandTableViewController *detailSand = [AddSandTableViewController new];
        [self.navigationController pushViewController:detailSand animated:YES];
    } else {
        DetailSandTableViewController *detailSand = [DetailSandTableViewController new];
        [self.navigationController pushViewController:detailSand animated:YES];
    }
}

#pragma - mark private
- (void)LayOutUI {
    [self editingBtn];
    
    [self.settingTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(FiveOffsetViewMargin);
        make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
        make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
        make.height.equalTo(@(SCREEN_HEIGHT));
    }];
}

#pragma mark - Event Response
//按钮事件
- (void)handleBtnAction:(UIButton *)button {
    NSInteger BtntTag = button.tag - 300;
    switch (BtntTag) {
        case 0:
//            button.selected = YES;
            if (button.selected == NO) {
                [button setTitle:@"保存" forState:0];
                [self addOneCell:button];
                button.selected = YES;
            } else {
                button.selected = NO;
                [button setTitle:@"编辑" forState:0];
                [self removeOneCell:button];
            }
            break;
        case 1:
            
            break;
        case 2:
            
            break;
            
        default:
            break;
    }
}

#pragma mark - Getters
- (NSMutableArray *)sandTitleArray {
    if (_sandTitleArray == nil) {
        _sandTitleArray = [NSMutableArray array];
    }
    return _sandTitleArray;
}

- (UIButton *)editingBtn {
    if (_editingBtn == nil) {
        _editingBtn = [UIButton btnWithTitle:@"编辑"];
        _editingBtn.tag = 300;
        [_editingBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithCustomView:_editingBtn];
        self.navigationItem.rightBarButtonItem = editItem;
    }
    return _editingBtn;
}

- (UITableView *)settingTable {
    if (_settingTable == nil) {
        _settingTable = [[UITableView alloc] init];
        _settingTable.rowHeight = (SCREEN_HEIGHT * 0.07) + 20;
        _settingTable.delegate = self;
        _settingTable.dataSource = self;
        [_settingTable registerClass:[SandTableCell class] forCellReuseIdentifier:identifier];
        _settingTable.backgroundColor = [UIColor clearColor];
        _settingTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_settingTable];
    }
    return _settingTable;
}



- (UIButton *)uploadBtn {
//    if (_uploadBtn == nil) {
        _uploadBtn = [UIButton buttonWithBackgroundColor:[UIColor colorWithHexString:@"#6ccac9"] textFont:TextFont20 textColor:[UIColor whiteColor] title:@"上传设置文件" cornerRadius:5];
        _uploadBtn.tag = 301;
        [_uploadBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:_uploadBtn];
//    }
    return _uploadBtn;
}

- (UIButton *)downloadBtn {
//    if (_downloadBtn == nil) {
        _downloadBtn = [UIButton buttonWithBackgroundColor:[UIColor colorWithHexString:@"#ff6c60"] textFont:TextFont20 textColor:[UIColor whiteColor] title:@"下载设置文件" cornerRadius:5];
        _downloadBtn.tag = 302;
        [_downloadBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:_downloadBtn];
//    }
    return _downloadBtn;
}

#pragma mark - private method
- (void)addOneCell:(id)sender {
    [self.settingTable beginUpdates];
    [self.sandTitleArray addObject:@"添加新沙盘"];
    NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:_sandTitleArray.count - 1 inSection:0]];
    [self.settingTable insertRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationNone];
    _settingTable.tableFooterView = [[UIView alloc]init];
    [_settingTable reloadData];
    [self.settingTable endUpdates];
}

- (void)removeOneCell:(id)sender {
    [self.settingTable beginUpdates];
    [self.sandTitleArray removeLastObject];
    NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:_sandTitleArray.count inSection:0]];
    [self.settingTable deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationNone];
    _settingTable.tableFooterView = [[UIView alloc]init];
    [_settingTable reloadData];
    [self.settingTable endUpdates];
}

@end
